import mongoose from "mongoose";

const MessageSchema = new mongoose.Schema(
  {
    messageId: { type: Number, index: true, unique: true, required: true },
    chatId: { type: mongoose.Schema.Types.ObjectId },
    userId: { type: mongoose.Schema.Types.ObjectId },
    date: { type: Number },
    mediaGroupId: { type: String },
    media: {
      id: { type: String },
      uniqueId: { type: String },
      size: { type: Number },
      type: { type: String },
      filename: { type: String },
      mimetype: { type: String },
      hash: { type: String, index: true },
    },
    createdAt: { type: Date },
    updatedAt: { type: Date },
  },
  {
    timestamps: { createdAt: true, updatedAt: true, currentTime: () => new Date() },
    virtuals: {
      user: {
        options: {
          ref: "User",
          localField: "userId",
          foreignField: "_id",
          justOne: true,
        },
      },
    },
  },
);

export const Message = mongoose.model("Message", MessageSchema);
